package com.hankol.newretail.adapter.details

import android.annotation.SuppressLint
import android.app.Activity
import android.view.*
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import cc.shinichi.library.view.photoview.OnPhotoTapListener
import cc.shinichi.library.view.photoview.PhotoView
import com.baselibrary.utils.GlideUtils
import com.hankol.newretail.R
import com.hankol.newretail.bean.goods_details.GoodsDetailsBanner
import com.hankol.newretail.widget.GoodsVideoView

/**
 * @author: 关忆北、
 * @date: 2022/8/2
 * @Des：商品详情封面 大图
 */
class GoodsCoverPopVpAdapter(var activity: Activity, var list: ArrayList<GoodsDetailsBanner>, var goodsVideoView: GoodsVideoView, var pos: Int) : PagerAdapter() {


    var mGsvGoodsMedialVideo: GoodsVideoView? = null
    private var mOnItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(mOnItemClickListener: OnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
        container.removeView(view as View)
    }

    @SuppressLint("SetTextI18n")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val inflate = LayoutInflater.from(container.context).inflate(R.layout.item_goods_details_media_pop, container, false)
        val ivGoodsMedialCover = inflate.findViewById<PhotoView>(R.id.iv_goods_medial_cover)
        mGsvGoodsMedialVideo = inflate.findViewById<GoodsVideoView>(R.id.gsv_goods_medial_video)

        if (list[position].coverType == GoodsDetailsBanner.GOODS_BANNER_VIDEO) {
            ivGoodsMedialCover.visibility = View.GONE
            mGsvGoodsMedialVideo?.visibility = View.VISIBLE
            mGsvGoodsMedialVideo?.setVideoCover(list[position].video, list[position].cover)
            mGsvGoodsMedialVideo?.setPlayMode(2)
            if (pos == position) {
                //说明点进来之前播放的是视频,继续播放
                goodsVideoView.rlGoodsVideoSpeed = mGsvGoodsMedialVideo?.rlGoodsVideoSpeed
                goodsVideoView.sbGoodsVideoSpeed = mGsvGoodsMedialVideo?.sbGoodsVideoSpeed
                goodsVideoView.mSbGoodsDetVoice = mGsvGoodsMedialVideo?.mSbGoodsDetVoice
                goodsVideoView.mTvGoodsVideoSpeed = mGsvGoodsMedialVideo?.mTvGoodsVideoSpeed
                goodsVideoView.mTvGoodsVideoTotalSpeed = mGsvGoodsMedialVideo?.mTvGoodsVideoTotalSpeed


                mGsvGoodsMedialVideo?.mVodPlayer = goodsVideoView.vodPlayer
                mGsvGoodsMedialVideo?.resultCode = goodsVideoView.resultCode
                goodsVideoView.vodPlayer?.setPlayerView(mGsvGoodsMedialVideo?.mTxclGoodsDetVideo)
                goodsVideoView.isVoice.let { mGsvGoodsMedialVideo?.isVoice(it) }
                goodsVideoView.vodPlayer?.resume()
                goodsVideoView.upPlayView(true)
                mGsvGoodsMedialVideo?.upPlayView(true)
                mGsvGoodsMedialVideo?.visibilityCover()
                mGsvGoodsMedialVideo?.setOnVoiceClickListener { b ->
                    goodsVideoView.isVoice = b
                }
            }


        } else {
            ivGoodsMedialCover.visibility = View.VISIBLE
            mGsvGoodsMedialVideo?.visibility = View.GONE
            ivGoodsMedialCover.setOnPhotoTapListener { _, _, _ -> mOnItemClickListener?.onItemClick(mGsvGoodsMedialVideo) }
            GlideUtils.loadImg_228(activity.applicationContext, list[position].cover, ivGoodsMedialCover)
        }
        container.addView(inflate)
        return inflate
    }

    interface OnItemClickListener {
        fun onItemClick(gsvGoodsMedialVideo: GoodsVideoView?)
    }

}